package com.example.perpetualcalendar.ui.fragment.joke;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.example.perpetualcalendar.data.network.AppSchedulerProvider;
import com.example.perpetualcalendar.domain.entity.Joke;
import com.example.perpetualcalendar.domain.usecase.JokeUseCase;
import com.example.perpetualcalendar.viewModel.BaseViewModel;

import io.reactivex.observers.DisposableObserver;

/**
 * @author langhua
 */
public class JokeViewModel extends BaseViewModel<JokeUseCase> {

    private MutableLiveData<String> mText;

    public JokeViewModel() {
        mText = new MutableLiveData<>();
        joke();
        mText.setValue("刷新试试");
    }

    @NonNull
    @Override
    protected JokeUseCase setUseCase() {
        return new JokeUseCase(AppSchedulerProvider.getInstance(), new JokeRepository());
    }


    public LiveData<String> getText() {
        return mText;
    }


    public void joke() {
        assert useCase != null;
        useCase.execute(new DisposableObserver<Joke>() {
            @Override
            public void onNext(Joke joke) {
                if(joke!=null&&joke.getResult()!=null){
                    mText.postValue(joke.getResult().getData().get(0).getContent());
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        },useCase.jokeApi());
    }

}